package java51_0607;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

//定义Book对象，包括名称，出版时间，页数，作者等，把对象写入文件，并读出

public class BookOopReader {

	public static void main(String[] args) {
		// 写对象 Books
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("D:\\书本信息.txt"));
			Books b1 = new Books("三国演义", "2007-8-7", 500, "罗贯中");
			Books b2 = new Books("红楼梦", "2008-12-11", 678, "曹雪芹");
			oos.writeObject(b1);
			oos.writeObject(b2);
			System.out.println("写入完成！！");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (oos != null)
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

		}

		// 读取对象 Books
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream("D:\\书本信息.txt"));

			Books b = (Books) ois.readObject();
			System.out.println(b);
			Books bb = (Books) ois.readObject();
			System.out.println(bb);

			// while (ois.readObject() != null) {
			// Books b = (Books) ois.readObject();
			// System.out.println(b);
			// }

			System.out.println("读取结束！！！");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ois != null)
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

	}

}
